/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dto;

import com.drew.imaging.ImageProcessingException;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ResourceBundle;
import javax.imageio.ImageIO;
import resources.ResourceManager;
import util.EXIFHelper;

/**
 * Represents an Image and its exif Data
 * 
 * @author steffen
 */
public class Image implements Serializable {    
    
    private EXIFData exif;
    private String path;
    
    public Image(String path) throws IOException {
        this(path, null);
    }
    
    /**
     * 
     * @param path
     * @param exif
     * @throws IllegalArgumentException
     * @throws IOException 
     */
    public Image(String path, EXIFData exif) throws IOException {
        if(path == null || path.isEmpty())
            throw new IllegalArgumentException(ResourceBundle.getBundle("gui/guilabels").getString("imageempty"));
        
        File f = new File(path);
        
        if(!f.exists() || !f.isFile()) 
            throw new IOException(ResourceBundle.getBundle("gui/guilabels").getString("imagenotexists"));
        
        this.path = path;
        this.exif = exif;
        
        if (exif == null) {
            try {
                this.exif = EXIFHelper.getEXIFData(path);
            } catch (ImageProcessingException ex) {
                //error reading exif data, handle it as there are none
            }
        }
    }
    
    public void importPicture() {
        ResourceManager resMan = new ResourceManager();
        path = resMan.copyPictureToProperties(path);
    }

    public EXIFData getExif() {
        return exif;
    }

    public void setExif(EXIFData exif) {
        this.exif = exif;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }
    
    public java.awt.Image getPicture() throws IOException {
        return ImageIO.read(new File(path));
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        
        builder.append("Imagepath: ").append(path).append("\n")
                .append("EXIF Data: ").append(exif.toString());
        
        return builder.toString();
    }
    
    /**
     * Builder class for an Image.
     */
    public static class Builder {
        
        /**
         * Create a new Image File out of a Filepath.
         * This file is only created if the filePath points 
         * to a valid file.
         * 
         * @param filePath The path to the file
         * @return The new created Image
         * @throws IOException Throws an IOException if the filePath doesnt
         *  point to a valid file.
         * @throws ImageProcessingException Throws an ImageProcessingException if 
         *  there was a prolem while reading the Exif data. This mostly happens if the 
         *  file isnt a image file.
         */
        public static Image build(String filePath) throws IOException, ImageProcessingException {
            Image image;
            File file = new File(filePath);
            
            if(!file.exists())
                throw new IOException("No file under");
        
            EXIFData exif = EXIFHelper.getEXIFData(filePath);
            
            image = new Image(filePath, exif);
            
            return image;
        }
    }
}
